package com.sdcqjy.property.base;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.sdcqjy.mylibrary.ActivityManager;
import com.sdcqjy.mylibrary.widget.LoadDialog;
import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.R;
import com.sdcqjy.property.activity.LoginActivity;


/**
 * Created by 磁磁帅 on 2017/8/16.
 * 描述：
 */
public class SimpleBaseActivity<T extends BaseContract.Presenter> extends AppCompatActivity {
    public static String isSceneTrans = "isSceneTrans";
    protected T presenter;

    public SimpleBaseActivity getActivity() {
        return this;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.getManager().addActivity(this);
    }

    protected int backBtn() {
        return R.id.imgBack;
    }

    private boolean isF = true;

    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus & isF) {
            isF = false;
            if (backBtn() != 0) {
                View imgBack = findViewById(backBtn());
                if (imgBack != null) {
                    imgBack.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View view) {
                            finish();
                        }
                    });
                }
            }
            asynRun();
        }
    }

    protected void asynRun() {
    }

    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) presenter.onDestroy();
        ActivityManager.getManager().closeActivity(this);
    }

    private LoadDialog dialog;

    public LoadDialog getDialog() {
        return dialog;
    }

    public void openLoadDialog() {
        if (dialog == null) {
            dialog = new LoadDialog(this);
            dialog.setColorSchemeResources(R.color.colorAccent, R.color.common_yellow);
            dialog.setOnCancelListener(d -> loadDialogCancel());
        }
        dialog.show();
    }

    protected void loadDialogCancel() {
//        if (request != null) {
//            request.stop();
//            request = null;}
    }

    public void dismissDialog() {
        if (dialog != null) dialog.dismiss();
    }

    public void applyError(int t) {
        String string = getString(t);
        applyError(string);
    }

    public void applyError(String t) {
        ShowToast.alertShortOfWhite(getActivity(), t);
    }

    protected boolean isLogin() {
        return isLogin(true);
    }

    protected boolean isLogin(boolean goLogin) {
        boolean login = AppLL.getAppLL().isLogin();
        if (goLogin && !login) {
            runOnUiThread(() -> LoginActivity.open(getActivity()));
        }
        return login;
    }

    // 开启浸入模式
    protected void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
//        View decorView = getWindow().getDecorView();
//        decorView.setFitsSystemWindows(!on);
    }

    private long oldTouchTime;

    //防止短时间内多次启动同一个Activity
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        long time = System.currentTimeMillis();
        long l = time - oldTouchTime;
        if (l < 0) {
            ShowToast.alertShortOfWhite(getActivity(), "手机时间异常，请检查");
            l = -l;
        }
        boolean isCanTouch;
        if (l < 500) isCanTouch = false;
        else {
            oldTouchTime = time;
            isCanTouch = true;
        }
        if (isCanTouch) super.startActivityForResult(intent, requestCode, options);
    }
}